import os
import unittest

import bpy

from .utils import ASSET_FOLDER_PATH
from ..utils import QueueUtils


class TestQueueRender(unittest.TestCase, QueueUtils):

    def add_element(self):
        self.assertEqual(bpy.ops.render.render_queue_add_element(directory=ASSET_FOLDER_PATH,
                                                                 filepath=ASSET_FOLDER_PATH,
                                                                 find_child_folder=True),
                         {"FINISHED"})

    def test_property(self):
        pref = self.pref
        render = bpy.context.scene.render
        cy = bpy.context.scene.cycles

        def prop(name, value, pr=None):
            p = pr if pr else pref
            setattr(p, name, value)
            self.assertEqual(getattr(p, name), value)

        prop("engine", "CYCLES", render)
        prop("filepath", r"D:\Users\EM1\Desktop", render)
        prop("use_adaptive_sampling", True, cy)
        prop("samples", 32, cy)
        prop("adaptive_min_samples", 8, cy)
        prop("denoiser", "OPENIMAGEDENOISE", cy)
        prop("device", "GPU", cy)
        prop("use_auto_tile", False, cy)

        # 插件属性
        
        prop("active_index", -1)
        prop("show_detailed_render_info", True)
        prop("split_queue_list", 0.5)
        prop("is_update", False)

        prop("use_resolution_percentage", True)
        prop("use_compositors_node", True)
        prop("use_samples", True)
        prop("use_engine", True)
        prop("use_threads", True)
        prop("use_parameter", True)
        prop("use_film", True)
        prop("use_format", True)
        prop("use_noise", True)
        prop("use_out", True)
        prop("use_performance", True)
        prop("use_color_management", False)
        prop("use_scene_resolution", False)

    def test_operator(self):
        self.add_element()
        for item in self.pref.render_items:
            item.selected = True

        for ops in (
                bpy.ops.render.render_queue_start_render,
                bpy.ops.render.render_queue_stop_render,
                bpy.ops.render.render_queue_clear,
                bpy.ops.render.render_queue_clear_cache,
                bpy.ops.render.render_queue_switch_finished,
        ):
            self.assertEqual(ops(), {"FINISHED"})

        self.assertEqual(bpy.ops.render.render_queue_set_state(state="IN_QUEUE"), {"FINISHED"})
        self.assertEqual(bpy.ops.render.render_queue_set_state(state="NOT_RENDER"), {"FINISHED"})
        self.assertEqual(bpy.ops.render.render_queue_set_state(state="RENDERED"), {"FINISHED"})

    def test_queue_render(self):
        self.add_element()
        self.assertEqual(bpy.ops.render.render_queue_start_render(), {"FINISHED"})
